﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MyMainSite.MetaWeblog;
using MyMainSite.Models;
using DotNetX;

namespace MyMainSite.Repositories
{
    public static class BlogEntryRepository
    {
        #region Public Static Methods

        public static int AddFromMetaWeblogPost(Post post, int blogId)
        {
            BlogEntry e = new BlogEntry();

            try
            {
                RichardsSiteEntities context = new RichardsSiteEntities();

                e.DateCreated = post.dateCreated == DateTime.MinValue ? DateTime.Now : post.dateCreated;
                e.BodyHtml = post.description;
                e.BodyText = post.description.StripHtml();
                e.FriendlyUrl = post.title.ToLower().Trim().Replace(' ', '-');
                e.Title = post.title;
                e.BlogId = blogId;

                context.BlogEntries.AddObject(e);

                context.SaveChanges();
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }

            return e.BlogEntryId;
        }

        public static bool UpdateFromMetaWeblogPost(Post post, int postId)
        {
            RichardsSiteEntities context = new RichardsSiteEntities();

            BlogEntry e = (from be in context.BlogEntries
                           where be.BlogEntryId == postId
                           select be).FirstOrDefault();

            if (e == null)
                return false;

            e.DateCreated = post.dateCreated == DateTime.MinValue ? DateTime.Now : post.dateCreated;
            e.BodyHtml = post.description;
            e.BodyText = post.description.StripHtml();
            e.FriendlyUrl = post.title.ToLower().Trim().Replace(' ', '-');
            e.Title = post.title;

            context.SaveChanges();

            return true;
        }

        public static BlogEntry GetById(int postId)
        {
            RichardsSiteEntities context = new RichardsSiteEntities();

            BlogEntry e = (from be in context.BlogEntries
                           where be.BlogEntryId == postId
                           select be).FirstOrDefault();

            return e;
        }

        public static List<BlogEntry> GetRecentEntriesByBlogId(int blogId, int number)
        {
            if (number <= 0)
                number = 1;

            RichardsSiteEntities context = new RichardsSiteEntities();

            var entries = (from be in context.BlogEntries
                           where be.BlogId == blogId
                           select be).OrderByDescending(be => be.DateCreated).Take(number);

            return entries.ToList();
        }

        public static bool DeleteById(int postId)
        {
            bool result = false;

            try
            {
                RichardsSiteEntities context = new RichardsSiteEntities();

                BlogEntry e = (from be in context.BlogEntries
                               where be.BlogEntryId == postId
                               select be).FirstOrDefault();

                context.BlogEntries.DeleteObject(e);
                context.SaveChanges();

                result = true;
            }
            catch (Exception ex)
            {
            }

            return result;
        }

        #endregion
    }
}